package com.rytong.tools.ui;

import com.rytong.tools.utils.Utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.AbsoluteLayout.LayoutParams;

/**
 * Views placed in the pop view as a list.
 * 
 * label_: display text
 * 
 * value_: transfer value
 * 
 * group_: If a select has a group attribute, once it's selected, other selects in the same page should be updated.
 * 
 * ref_: If a option has a ref attribute, it should not be displayed in the select unless its reference group has been
 * selected.
 */
public  class LPSelectOption extends Component {
    private static final int ROW_TOUCH_BG_COLOR = 0xff2762e1;

    String selectSort_;
    String sortKey_;
    String group_;
    String ref_;
    String select_;
    String[] relaEnable_;
    String[] relaDisable_;
    String[] relaVisible_;
    String[] relaInvisible_;
    LPSelect lpselect_;
    TextPaint paint_;
    boolean isSelected_;// 默认选中�?option selected=‘selected�?value='1'>xxxx</option>�?

    public class MyOption extends TextView implements CompositedComponent {

        public MyOption(Context context, String value) {
            super(context);
            // TODO Auto-generated constructor stub
            setText(value);
            setFocusable(true);
            setFocusableInTouchMode(true);
        }

        // Override
        // protected void onDraw(Canvas canvas) {
        // // 设置默认字体对其方式为居�?
        // if (this.isFocused()) {
        // paint_.setColor(ROW_TOUCH_BG_COLOR);
        // canvas.drawRect(0, 0, getWidth(), getHeight(), paint_);
        // }
        // float fontSize = fontSize();
        // paint_.setTextSize(fontSize);
        // String textAlign = null;
        // if (lpselect_ != null && lpselect_.cssStyle() != null) {
        // textAlign = lpselect_.cssStyle().textAlign_;
        // }
        // float labelWidth = paint_.measureText(label());
        // float x = 0;
        // float y = (getHeight() + fontSize) / 2;
        // if ("center".equalsIgnoreCase(textAlign)) {
        // x = (getWidth() - labelWidth) / 2;
        // } else if ("right".equalsIgnoreCase(textAlign)) {
        // x = getWidth() - labelWidth;
        // } else { // it's left or not set.
        // x = 0;
        // }
        // paint_.setColor(textColor());
        // canvas.drawText(label(), x, y, paint_);
        // }

        /**
         * 响应触摸
         * */
        public boolean onTouchEvent(MotionEvent event) {
            int touchEvent = event.getAction();
            switch (touchEvent) {
            case MotionEvent.ACTION_DOWN:
                this.requestFocus();
                this.invalidate();
                break;
            case MotionEvent.ACTION_UP:
                lpselect_.setCurrentMenu(lpselect_.childrenList_.indexOf(this));
                setSelectView((LPSelectOption) composited());
                onClick((String) composited().property_.get("onclick"));
                lpselect_.refresh();
                break;
            default:
                super.onTouchEvent(event);
            }
            return true;
        }

        @Override
        public Component composited() {
            // TODO Auto-generated method stub
            return LPSelectOption.this;
        }

    }

    /**
     * @return
     */
    private int textColor() {
        int textColor = lpselect_ != null && lpselect_.cssStyle() != null ? lpselect_.fgColor() : 0;
        if (textColor == 0) {
            textColor = SELECT_DEFAULT_TEXT_COLOR;
        }
        return textColor;
    }

    public void mouldH() throws Exception {
        super.mouldH();
        Paint paint = getPaint();
        if (null == paint)
            paint = new Paint();
        // 设置字体或者绘制效果平滑
        paint.setAntiAlias(true);
        if (null != cssStyle_) {
            String bgImageUrl = (String) cssStyle_.getProperty(CssStyle.BGIMAGEURL);
            if (null != bgImageUrl) {
                // 根据样式定义控件选中图片
                unFocusImg_ = Utils.getBitmapFromResources(getContext(), bgImageUrl);
                if (null != unFocusImg_) {
                    unFocusImg_ = Bitmap.createScaledBitmap(unFocusImg_, width_, height_, true);
                }
                // 用户点击的图片也应该有样式定义，此处暂时设置为未点击时的颜色效果
                focusImg_ = Utils.getGrayBitmap(unFocusImg_);
            }
        } else {
            // 根据widget.xml设置控件的属�?
            String temp = getAttribute("FontSize_");
            if (null == temp || temp.equals(""))
                temp = "15";
            paint.setTextSize(Integer.parseInt(temp));
            temp = getAttribute("FrontColor_");
            if (null == temp || temp.equals(""))
                temp = "0x000000";
            paint.setColor(Color.parseColor(temp));

        }
    }

    /**
     * @return
     */
    String label() {
        MyOption mo = (MyOption) realView_;
        String label = mo.getText().toString();
        return label;
    }

    /**
             * 
             */
    public void dropdownMenuClickItem() {
        setContentText(label());
        postInvalidate();
    }

    /**
     * 
     */
    private void setSelectView(LPSelectOption lpso) {
        try {
            lpselect_.setContentText(lpso.label(), (String) lpso.getPropertyByName("value"));
            lpselect_.ad_.dismiss();
            LPSelect.ISPOPING = false;
        } catch (Exception e) {
            Utils.printException(e);
        }
    }

    @Override
    public void initRealView(Activity activity, String value) {
        realView_ = new LPSelectOption.MyOption(activity, value);
        String selected = (String) getPropertyByName("selected");
        if (selected.equals("selected")) {
            isSelected_ = true;
        }
    }

    public void setParent(Component component) {
        // TODO Auto-generated method stub
        lpselect_ = (LPSelect) component;
    }

}
